jQuery(function ($) {
    var oTable1 = $('#table-list-1').dataTable(
        {
            "aoColumnDefs"  : [
                {
                    aTargets: [0],
                    fnRender: function (o) { return '<label><input type="checkbox" class="ace" value="'+o.aData.checkbox+'" /><span class="lbl"></span></label>'} ,
                    sClass: 'center'
                },
                {
                    aTargets: [7],
                    fnRender: function (o) {
                        return '' +
                            '<div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">' +
                            '<a class="blue" href="#'+o.aData.action+'"><i class="icon-zoom-in bigger-130"></i></a>' +
                            '<a class="green" id="'+o.aData.action+'" href="#modal-form" role="button" ><i class="icon-pencil bigger-130"></i> </a>' +
                            '<a class="red" href="#'+o.aData.action+'"><i class="icon-trash bigger-130"></i></a>' +
                            '</div>' +
                            '<div class="visible-xs visible-sm hidden-md hidden-lg">' +
                            '   <div class="inline position-relative">' +
                            '       <button class="btn btn-minier btn-yellow dropdown-toggle" data-toggle="dropdown"><i class="icon-caret-down icon-only bigger-120"></i></button>' +
                            '       <ul class="dropdown-menu dropdown-only-icon dropdown-yellow pull-right dropdown-caret dropdown-close">' +
                            '           <li><a href="#" class="tooltip-info" data-rel="tooltip" title="View"><span class="blue"><i class="icon-zoom-in bigger-120"></i></span></a></li>' +
                            '           <li><a href="#" class="tooltip-success" data-rel="tooltip" title="Edit"><span class="green"><i class="icon-edit bigger-120"></i></span></a></li>' +
                            '           <li><a href="#" class="tooltip-error" data-rel="tooltip" title="Delete"><span class="red"><i class="icon-trash bigger-120"></i></span></a></li>' +
                            '       </ul>' +
                            '   </div>' +
                            '</div>';
                    }
                }
            ],
            "aoColumns": [
                { "mData": "checkbox" ,"bSortable": false,sClass: 'center'},
                { "mData": "domain_id","bSortable": false },
                { "mData": "site_name" },
                { "mData": "site_domain" },
                { "mData": "create_time" },
                { "mData": "site_type" },
                { "mData": "site_description" ,"bSortable": false},
                { "mData": "action" , "bSortable": false,"bSearchable": false}
            ],
            "bServerSide": true,
            "sAjaxSource": "/admin/spiderSite/Ajax_list",
            "fnServerData": function ( sSource, aoData, fnCallback ) {
                $.getJSON( sSource, aoData, function (json) {
                    var temp = [];
                    $(json['aaData']).each(function(i,val) {
                        temp.push({
                            domain_id: val.domain_id,
                            site_name: val.site_name,
                            site_domain: val.site_domain,
                            create_time: val.create_time,
                            site_type: val.site_type,
                            site_description: val.site_type,
                            action: val.domain_id,
                            checkbox: val.domain_id
                        });
                    });
                    json['aaData'] = temp;
                    fnCallback(json)
                } );
            }
        }
    );

    $('#table-list-1_info').before('<div class="col-sm-6" style="float:left;width: 20%;"><a class="red" href="#" id="table-list-1-delete-all"><i class="icon-trash bigger-130"></i>全部删除</a></div>');

    $('#table-list-1-delete-all').click(
        function()
        {
            alert('xxx');
            return false;
        }
    );

    $('table th input:checkbox').on('click', function () {
        var that = this;
        $(this).closest('table').find('tr > td:first-child input:checkbox')
            .each(function () {
                this.checked = that.checked;
                $(this).closest('tr').toggleClass('selected');
            });

    });


    $('[data-rel="tooltip"]').tooltip(
        {
            placement: tooltip_placement
        }
    );
    function tooltip_placement(context, source) {
        var $source = $(source);
        var $parent = $source.closest('table')
        var off1 = $parent.offset();
        var w1 = $parent.width();

        var off2 = $source.offset();
        var w2 = $source.width();

        if (parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2)) return 'right';
        return 'left';
    }

});
